//
//  YKPerformanceExchangeViewController.m
//  YunKang
//
//  Created by leoking870 on 2017/10/15.
//  Copyright © 2017年 leoking870. All rights reserved.
//

#import "YKPerformanceExchangeViewController.h"
#import "YKClientListHeaderView.h"
#import "YKRequestPerformanceExchangeViewController.h"
#import "YKPerformanceExchangeItem.h"
#import "YKRequestExchangeViewController.h"
@interface YKPerformanceExchangeViewController ()

@end

@implementation YKPerformanceExchangeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"业绩兑换管理";
    
    YKClientListHeaderView* headerView = [[YKClientListHeaderView alloc]initWithTitles:@[@"申请单号",@"申请积分",@"申请日期",@"状态"] weights:@[@1,@1,@1,@1]];
    [self.view addSubview:headerView];
    headerView.backgroundColor = [UIColor tf_colorWithHexString:@"#1EADFC"];
    headerView.textColor = [UIColor whiteColor];
    
    headerView.textFont = [UIFont systemFontOfSize:16];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.trailing.equalTo(self.view);
    }];
    
    UIButton *confirmButton = [[UIButton alloc]init];
    [confirmButton setTitle:@"新增业绩兑换" forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:TFSTYLE(buttonEnableBackgroundColor)];
    [confirmButton setTitleColor:TFSTYLE(buttonEnableTitleColor) forState:UIControlStateNormal];
    [self.view addSubview:confirmButton];
    [confirmButton addTarget:self action:@selector(onAddItemTapped:) forControlEvents:UIControlEventTouchUpInside];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.trailing.equalTo(self.view);
        make.height.mas_equalTo(40);
    }];
    confirmButton.titleLabel.font = [UIFont systemFontOfSize:14];
    confirmButton.layer.cornerRadius = 5;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.view);
        make.top.equalTo(headerView.mas_bottom);
        make.bottom.equalTo(confirmButton.mas_top);
    }];

//#pragma mark - MOCK
//    TFTableViewSection* section = [TFTableViewSection section];
//    section.headerHeight = .1f;
//    section.footerHeight = .1f;
//    @weakify(self)
//    for (int i = 0; i < 10; ++i) {
//        YKPerformanceExchangeItem* item = [YKPerformanceExchangeItem item];
//        [section addItem:item];
//        item.hideSeparator = YES;
//        //        item.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//        item.selectionHandler = ^(__kindof TFTableViewItem *item, NSIndexPath *indexPath) {
//            YKRequestPerformanceExchangeViewController *vc = [[YKRequestPerformanceExchangeViewController alloc]init];
//            [self_weak_.navigationController pushViewController:vc animated:YES];
//        };
//    }
//
//    [self.dataSource.manager addSection:section];
    
    self.dataSource.totalPageKey = @"page.totalpage";
    self.dataSource.currentPageKey = @"curpage";
    [self startLoadData];
    
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (self.dataSource.manager.sections.count > 0) {
        [self startLoadDataSilently];
    }
}

- (NSDictionary *)pageViewControllerParameters {
    return @{
             kTFPageViewControllerItemClass: [YKPerformanceExchangeItem class],
             kTFPageViewControllerModelClass:[YKPerformanceExchangeModel class],
             kTFPageViewControllerRequestURL:@"achievementPointsExchange/achievementPointsExchangeList.htm",
             kTFPageViewControllerDataKeyPath:@"list",
             kTFPageViewControllerRequestParameter:@{@"c_name":[self searchText]}
             };
}

- (void)beginSearchWithText:(NSString *)text {
    [self startLoadData];
}

- (void)customizeItem:(TFTableViewItem *)item {
    @weakify(self)
    item.selectionHandler = ^(__kindof YKPerformanceExchangeItem *item, NSIndexPath *indexPath) {
        YKRequestPerformanceExchangeViewController *vc = [[YKRequestPerformanceExchangeViewController alloc]init];
        vc.m_id = item.model.ID;
        [self_weak_.navigationController pushViewController:vc animated:YES];
    };
}

- (void)onAddItemTapped:(id)sender {
    YKRequestExchangeViewController *vc = [[YKRequestExchangeViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}


@end
